/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.user.provider.hibernate3.ahxu.impl;

import com.opensymphony.user.provider.ejb.util.Base64;
import com.opensymphony.user.provider.ejb.util.PasswordDigester;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.BaseHibernateEntity;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.HibernateGroup;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.HibernateUser;

import java.util.*;


/**
 * @author <a href="mailto:matthew@metissian.com">Matthew E. Porter</a>
 */
public class HibernateUserImpl extends BaseHibernateEntity implements HibernateUser {
    //~ Instance fields ////////////////////////////////////////////////////////

    private Set groups;
    private String passwordHash;

    //~ Methods ////////////////////////////////////////////////////////////////

    public List getGroupList() {
        ArrayList groupList = new ArrayList();

        if ((this.groups != null) && (this.groups.size() > 0)) {
            Iterator groupIter = this.groups.iterator();

            while (groupIter.hasNext()) {
                HibernateGroup group = (HibernateGroup) groupIter.next();
                groupList.add(group);
            }
        }

        return groupList;
    }

    public List getGroupNameList() {
        ArrayList groupNameList = new ArrayList();

        if ((this.groups != null) && (this.groups.size() > 0)) {
            Iterator groupIter = this.groups.iterator();

            while (groupIter.hasNext()) {
                HibernateGroup group = (HibernateGroup) groupIter.next();
                groupNameList.add(group.getName());
            }
        }

        return groupNameList;
    }

    public void setGroups(Set groups) {
        this.groups = groups;
    }

    public Set getGroups() {
        return groups;
    }

    public void setPassword(String password) {
        setPasswordHash(createHash(password));
    }

    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public String getPasswordHash() {
        return passwordHash;
    }

    public void addGroup(HibernateGroup group) {
        if (this.groups == null) {
            this.groups = new HashSet(1);
        }

        this.groups.add(group);
    }

    /**
     * Verify that supplied password matches that of user.
     *
     */
    public boolean authenticate(String password) {
        if ((password == null) || (getPasswordHash() == null) || (password.length() == 0)) {
            return false;
        } else {
            return compareHash(getPasswordHash(), password);
        }
    }

    public void removeGroup(HibernateGroup group) {
        if ((this.groups != null) && (this.groups.contains(group))) {
            this.groups.remove(group);
        }
    }

    private boolean compareHash(String hashedValue, String unhashedValue) {
        return hashedValue.equals(createHash(unhashedValue));
    }

    private String createHash(String original) {
        byte[] digested = PasswordDigester.digest(original.getBytes());
        byte[] encoded = Base64.encode(digested);

        return new String(encoded);
    }
}
